package com.funo.wcity.app.filter;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.funo.wcity.app.entity.LoginUser;
import com.funo.wcity.app.utils.Constant;
import com.funo.wcity.app.utils.StringUtil;
/**
 * 
 * Copyright (c) 2012,　福建福诺移动通信技术有限公司 All rights reserved。
 * 文件名称：AppFilter.java 是否登入过滤
 * @author 李刚
 * @date 2012-05-31 下午11:04
 * @version 1.0 修改记录： 修改时间： 修 改 人： 修改内容：
 */
@SuppressWarnings("all")
public class LoginFilter implements Filter{
	private  final Log logger = LogFactory.getLog(LoginFilter.class);
	private  Properties properties;
	public void destroy() {}
	public void init(FilterConfig filter) throws ServletException {
		 
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filter) throws IOException, ServletException {
		try {
			HttpServletRequest req = (HttpServletRequest)request;
			//进入手动操作,首页请求 不过滤
			String method=StringUtil.reTrimByString(req.getRequestURI());
			if(method.indexOf("/login.do")==-1){
				LoginUser loginUser=(LoginUser)req.getSession().getAttribute(Constant.LOGIN_USER);
				if(loginUser!=null && !StringUtil.isEmpty(loginUser.getUsername()) && !StringUtil.isEmpty(loginUser.getPassword())){
					filter.doFilter(request,response);
				}else{
					((HttpServletResponse)response).sendRedirect("/mailClient");
				}
			}else{
				filter.doFilter(request,response);
			}
			 
		} catch (Exception e) {
			logger.error("抱歉，验证登入异常：" + e.getMessage(), e);
		}
	}
}
